
#include "Customer.h"
#include "CustomersParser.h"

#include <assert.h>
#include <QRegExp>
#include <QDir>
#include <QDebug>

using namespace ecocity;

//---------------------------------------------------------

bool
CustomerParser::parse( const QString& file_customers_csv,
                       std::vector< Customer >& customers )
{
    customers.clear( );

    // expects '/' as directory separator regardless of operating system.
    QFile file_customers( file_customers_csv );

    bool result = true;

    if ( !file_customers.open( QFile::ReadOnly ) )
    {
        assert( 0 );
        result = false;
    }

    // Important: Use separator ';' cause filds like address might contain ','
    if( !parse_file( file_customers, customers, ';' ) )
    {
        assert( 0 );
        result = false;
    }

    file_customers.close( );

    return result;
}

//---------------------------------------------------------

bool
CustomerParser::parse_file( QFile& file,
                            std::vector< Customer >& customers,
                            const char separator )
{
    QTextStream stream( &file );

    while ( !stream.atEnd( ) )
    {
        QString line = stream.readLine( );

        while( line.startsWith( '#' ) )
        {
            line = stream.readLine( );
        }

        std::vector<int> separators =
                separators_in_line( line, separator );

        // list should contain fixed number of separators
        //      id,Name,Address,Price Type,number invoices,total sold
        // i.e. 1,Raul Ferrandez, Skalitzer Str. 59A,Friends,5,150
        if( separators.size( ) != CustomersModel::s_columns -1 )
        {
            assert( 0 );
            return false;
        }

        parse_line( line, customers, separator );
    }

    return true;
}

//---------------------------------------------------------

void
CustomerParser::parse_line( const QString &line,
                            std::vector< Customer >& customers,
                            const char separator )
{
    Customer customer = to_customer( line, separator );

    customers.push_back( customer );
}

//---------------------------------------------------------

Customer
CustomerParser::to_customer( const QString &line, const char separator )
{
    int id = line.section( separator,
                           CustomersModel::CUSTOMER_ID,
                           CustomersModel::CUSTOMER_ID ).toInt( );

    QString name = line.section( separator,
                                 CustomersModel::CUSTOMER_NAME,
                                 CustomersModel::CUSTOMER_NAME );

    QString address = line.section( separator,
                                    CustomersModel::CUSTOMER_ADDRESS,
                                    CustomersModel::CUSTOMER_ADDRESS );

    QString phone = line.section( separator,
                                  CustomersModel::CUSTOMER_PHONE,
                                  CustomersModel::CUSTOMER_PHONE );

    QString nif = line.section( separator,
                                CustomersModel::CUSTOMER_NIF,
                                CustomersModel::CUSTOMER_NIF );

    QString price_type_str = line.section( separator,
                                        CustomersModel::CUSTOMER_PRICE_TYPE,
                                        CustomersModel::CUSTOMER_PRICE_TYPE );

    Customer::TYPE price_type = Customer::to_customer_type( price_type_str );

    Customer customer( id,           // customer_id
                       name,         // customer_name
                       address,      // customer_description
                       phone,        // customer phone
                       nif,          // customer nif
                       price_type ); // customer_price_type

    return customer;
}

//---------------------------------------------------------

std::vector<int>
CustomerParser::separators_in_line( const QString& line, const char separator )
{
    std::vector<int> separators;

    int index = line.indexOf( separator, 0 );

    while ( index != -1 )
    {
        separators.push_back( index );
        index = line.indexOf( separator, index+1 );
    }

    return separators;
}

//---------------------------------------------------------
